﻿// NitroFileFrameReader.cs:
// Reader for Nitro (Nintendo DS) file frames.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License.
// See LICENSE for more information.

using System;
using System.IO;

namespace SharpNitroLib.NARC
{
	/// <summary>
	/// Reader for Nitro (Nintendo DS) file frames.
	/// </summary>
	public class NitroFileFrameReader
	{
	
		/// <summary>
		/// Create new NitroFileFrameReader.
		/// </summary>
		/// <param name="stream">Source stream.</param>
	    public NitroFileFrameReader(Stream stream) : this(new BinaryReader(stream)){}
	
	    /// <summary>
		/// Create new NitroFileFrameReader.
		/// </summary>
		/// <param name="stream">Binary reader on source.</param>
	    public NitroFileFrameReader(BinaryReader reader)
	    {
	        _reader = reader;
	    }
	
	    /// <summary>
	    /// Binary reader for source.
	    /// </summary>
	    private BinaryReader _reader;
	
	    /// <summary>
	    /// Read single NitroFileFrame from source.
	    /// </summary>
	    /// <returns></returns>
	    public NitroFileFrame Read()
	    {
	        var nitroFileHeader = new NitroFileFrame();
	
	        nitroFileHeader.Magic = _reader.ReadUInt32();
	        var frameSize = _reader.ReadUInt32();
	        var contentLength = (int)frameSize - 8;
	        nitroFileHeader.FrameData = _reader.ReadBytes(contentLength);
	
	        return nitroFileHeader;
	    }
	}
}
